package com.yzt.minemodule.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.config.AppConfig;
import com.yzt.zhuju.permission.PermissionsUtils;
import com.yzt.zhuju.utils.PermissionUtils;
import com.yzt.zhuju.utils.SystemUtil;
import com.yzt.minemodule.R;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 权限列表
 */
public class PermissionsListActivity extends BaseActivity {

    private ItemAdapter mItemAdapter;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, PermissionsListActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_system_permissions;
    }

    @Override
    public void installViews() {
        mTitleBar.setTitle(getString(R.string.system_permission_manager));

        RecyclerView mListRv = findViewById(R.id.list_rv);
        mItemAdapter = new ItemAdapter();
        mListRv.setAdapter(mItemAdapter);

        mItemAdapter.setOnItemClickListener((adapter, view, position) -> {
            ItemEntity o = (ItemEntity) adapter.getData().get(position);
            if (o.type == 1) {
                // 悬浮窗
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + AppConfig.context.getPackageName()));
                mActivity.startActivityForResult(intent, 0x10000);
            } else if (o.type == 2) {
                // 通知栏
                PermissionUtils.openNotificationPermissionSetting(this);
            } else {
                PermissionsUtils.getInstance().start(mActivity);
            }
        });

//        ((TextView) findViewById(R.id.label)).setText(Html.fromHtml("你可以在<font color='#2FC2FF'>《权限说明》</font>中了解到权限的详细应用说明"));
//        findViewById(R.id.label).setOnClickListener(v -> PermissionsDetailsActivity.start(mActivity));
    }

    @Override
    protected void onResume() {
        super.onResume();
        List<ItemEntity> list = new ArrayList<>();
        list.add(new ItemEntity(getString(R.string.permission_storage_information), PermissionsUtils.getInstance().lacksPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)));
        list.add(new ItemEntity(getString(R.string.permission_camera_information), PermissionsUtils.getInstance().lacksPermission(mActivity, Manifest.permission.CAMERA)));
        list.add(new ItemEntity(getString(R.string.permission_microphone_information), PermissionsUtils.getInstance().lacksPermission(mActivity, Manifest.permission.RECORD_AUDIO)));
        list.add(new ItemEntity(getString(R.string.permission_phone_state_information), PermissionsUtils.getInstance().lacksPermission(mActivity, Manifest.permission.READ_PHONE_STATE)));
        list.add(new ItemEntity(getString(R.string.permission_float_window_information), Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(mActivity), 1));
        //list.add(new ItemEntity(getString(R.string.permission_contact_information), PermissionsUtils.getInstance().lacksPermission(mActivity, Manifest.permission.READ_CONTACTS) && PermissionsUtils.getInstance().lacksPermission(mActivity, Manifest.permission.WRITE_CONTACTS)));
        list.add(new ItemEntity(getString(R.string.notification), SystemUtil.isNotificationEnable(), 2));
        mItemAdapter.setList(list);
    }

    static class ItemAdapter extends BaseQuickAdapter<ItemEntity, BaseViewHolder> {

        public ItemAdapter() {
            super(R.layout.item_system_permissions);
        }

        @Override
        protected void convert(BaseViewHolder helper, ItemEntity item) {
            helper.setText(R.id.title_tv, item.title);
            helper.setText(R.id.content_tv, item.open ? getContext().getString(R.string.allow_access) : getContext().getString(R.string.not_allow_access));
            helper.setTextColor(R.id.content_tv, Color.parseColor(item.open ? "#C4C8D0" : "#FF6550"));
        }
    }

    static class ItemEntity {

        String title;
        boolean open;
        int type;

        public ItemEntity(String title, boolean open) {
            this.title = title;
            this.open = open;
        }

        public ItemEntity(String title, boolean open, int type) {
            this.title = title;
            this.open = open;
            this.type = type;
        }
    }
}
